package com.canaan.business.service.basic.impl;

import java.util.Date;
import java.util.List;

import com.canaan.business.domain.basic.Container;
import com.canaan.business.domain.basic.ContainerBarcode;
import com.canaan.business.mapper.basic.ContainerMapper;
import com.canaan.business.service.basic.IContainerBarcodeService;
import com.canaan.business.service.basic.IContainerService;
import com.canaan.common.constant.PrefixConstants;
import com.canaan.common.exception.ServiceException;
import com.canaan.common.utils.DateUtils;
import com.canaan.common.utils.SecurityUtils;
import com.canaan.common.utils.StringUtils;
import com.canaan.common.utils.TableNoUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
/**
 * WMS 容器Service业务层处理
 *
 * @author ruoyi
 * @date 2023-08-01
 */
@Service
public class ContainerServiceImpl implements IContainerService {
    @Resource
    private ContainerMapper containerMapper;
    @Resource
    private IContainerBarcodeService containerBarcodeService;

    /**
     * 查询WMS 容器
     *
     * @param id WMS 容器主键
     * @return WMS 容器
     */
    @Override
    public Container selectContainerById(Long id) {
        return containerMapper.selectContainerById(id);
    }

    /**
     * 查询WMS 容器列表
     *
     * @param container WMS 容器
     * @return WMS 容器
     */
    @Override
    public List<Container> selectContainerList(Container container) {
        return containerMapper.selectContainerList(container);
    }

    /**
     * 新增WMS 容器
     *
     * @param container WMS 容器
     * @return 结果
     */
    @Override
    public int insertContainer(Container container) {
        if(StringUtils.isBlank(container.getNo())){
            container.setNo(PrefixConstants.CONTAINER+TableNoUtil.getNo(Container.class));
        }
        container.setCreateBy(SecurityUtils.getUsername());
        container.setCreateTime(DateUtils.getNowDate());
        return containerMapper.insertContainer(container);
    }

    /**
     * 批量新增WMS 容器
     *
     * @param containerList WMS 容器列表
     * @return 结果
     */
    @Override
    public int batchInsertContainer(List<Container> containerList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (Container container : containerList){
            if(StringUtils.isBlank(container.getNo())){
                container.setNo(TableNoUtil.getNo(Container.class));
            }
            container.setCreateBy(username);
            container.setCreateTime(currerTime);
        }
        int insertNum = 0;
        for (int i=0; i<containerList.size();) {
            int endIndex = i+500;
            if (endIndex > containerList.size()) {
                endIndex = containerList.size();
            }
            insertNum = insertNum + containerMapper.batchInsertContainer(containerList.subList(i, endIndex));
            i = endIndex;
        }
        return insertNum;
    }

    /**
     * 修改WMS 容器
     *
     * @param container WMS 容器
     * @return 结果
     */
    @Override
    public int updateContainer(Container container) {
        container.setUpdateBy(SecurityUtils.getUsername());
        container.setUpdateTime(DateUtils.getNowDate());
        return containerMapper.updateContainer(container);
    }

    /**
     * 批量修改WMS 容器
     *
     * @param containerList WMS 容器列表
     * @return 结果
     */
    @Override
    public int batchUpdateContainer(List<Container> containerList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (Container container : containerList){
            container.setUpdateBy(username);
            container.setUpdateTime(currerTime);
        }
        int updateNum = 0;
        for (int i=0; i<containerList.size();) {
            int endIndex = i+500;
            if (endIndex > containerList.size()) {
                endIndex = containerList.size();
            }
            updateNum = updateNum + containerMapper.batchUpdateContainer(containerList.subList(i, endIndex));
            i = endIndex;
        }
        return updateNum;
    }

    /**
     * 批量删除WMS 容器
     *
     * @param ids 需要删除的WMS 容器主键集合
     * @return 结果
     */
    @Override
    public int deleteContainerByIds(Long[] ids) {
        return containerMapper.deleteContainerByIds(ids);
    }

    /**
     * 删除WMS 容器信息
     *
     * @param id WMS 容器主键
     * @return 结果
     */
    @Override
    public int deleteContainerById(Long id) {
        return containerMapper.deleteContainerById(id);
    }


    /**
     * 查询WMS 容器
     *
     * @param no 编号
     * @return WMS 容器
     */
    @Override
    public Container selectContainerByNo(String no) {
        Container container = containerMapper.selectContainerByNo(no);
        if (container == null){
            throw new ServiceException("编号对应容器不存在");
        }
        // 查询容器关联的条码
        ContainerBarcode searchBarcodeRelParam = new ContainerBarcode();
        searchBarcodeRelParam.setContainerNo(no);
        container.setContainerBarcodeList(containerBarcodeService.selectContainerBarcodeList(searchBarcodeRelParam));

        return container;
    }

    /**
     * 批量删除WMS 容器
     *
     * @param nos 需要删除的编号集合
     * @return 结果
     */
    @Override
    public int deleteContainerByNos(String[] nos) {
        return containerMapper.deleteContainerByNos(nos);
    }

    /**
     * 删除WMS 容器信息
     *
     * @param no 编号
     * @return 结果
     */
    @Override
    public int deleteContainerByNo(String no) {
        return containerMapper.deleteContainerByNo(no);
    }
}
